Summary
Two-Factor Authentication (2FA) বা Two-Step Verification একটি নিরাপত্তা ব্যবস্থা, যেখানে ব্যবহারকারীকে তার পরিচয় যাচাই করতে একটি অতিরিক্ত প্রমাণীকরণ স্তর ব্যবহার করতে হয়, যেমন Google Authenticator। এটি Time-based One-Time Password (TOTP) সিস্টেম ব্যবহার করে। Spring Security তে Google Authenticator ব্যবহার করে 2FA কনফিগার করার ধাপগুলো হলো:
- ডিপেনডেন্সি যোগ করা: pom.xml ফাইলে প্রয়োজনীয় ডিপেনডেন্সি যোগ করতে হবে, যেমন Spring Boot Starter Security এবং Google Authenticator।
- Google Authenticator সেটআপ:
- একটি সিক্রেট কী তৈরি করা, যা ব্যবহারকারী Google Authenticator অ্যাপে স্ক্যান করবে।
- QR কোড দেখানো, যাতে ব্যবহারকারী সেটি স্ক্যান করে সিক্রেট কী সম্পূর্ণ করতে পারে।
- OTP যাচাই করা, যেখানে ব্যবহারকারী Google Authenticator থেকে OTP প্রদান করবে এবং তা যাচাই হবে।
- Spring Security কনফিগারেশন: লগইন প্রক্রিয়াতে 2FA যাচাই অন্তর্ভুক্ত করতে হবে।
TwoFactorAuthFilter ব্যবহার করে 2FA যাচাই প্রক্রিয়া সম্পন্ন হবে। ব্যবহারকারীর দেওয়া OTP যাচাই করা হবে এবং সঠিক হলে লগইন হবে; অন্যথায়, ত্রুটি পেজে রিডাইরেক্ট হবে।
উপসংহারে, Google Authenticator ব্যবহার করে 2FA কনফিগারেশন Spring Security অ্যাপ্লিকেশনের জন্য শক্তিশালী দ্বৈত প্রমাণীকরণ ব্যবস্থা প্রদান করে, যা ব্যবহারকারীর সুরক্ষা বাড়ায়।
Two-Factor Authentication (2FA) বা Two-Step Verification হলো একটি নিরাপত্তা ব্যবস্থা যেখানে ব্যবহারকারীকে একটি অতিরিক্ত প্রমাণীকরণ স্তর (যেমন Google Authenticator) ব্যবহার করে তার পরিচয় যাচাই করতে হয়। এটি একটি Time-based One-Time Password (TOTP) সিস্টেম ব্যবহার করে, যা একটি নির্দিষ্ট সময়ের জন্য একক পাসওয়ার্ড তৈরি করে।
Spring Security তে Google Authenticator ব্যবহার করে 2FA কনফিগার করার জন্য কয়েকটি ধাপ অনুসরণ করতে হবে। আমরা এখানে একটি সহজ উদাহরণ দিয়ে দেখাবো কিভাবে 2FA কনফিগার করতে হয়।
ধাপ ১: প্রয়োজনীয় ডিপেনডেন্সি যোগ করা
প্রথমে, pom.xml ফাইলে প্রয়োজনীয় ডিপেনডেন্সি যোগ করতে হবে।
<dependencies>
<!-- Spring Boot Starter Security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- Spring Boot Starter Web (for building REST API) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Google Authenticator dependency for 2FA -->
<dependency>
<groupId>com.warrenstrange</groupId>
<artifactId>googleauth</artifactId>
<version>1.5.0</version>
</dependency>
</dependencies>
googleauth লাইব্রেরিটি ব্যবহারকারীদের জন্য TOTP (Time-based One-Time Password) জেনারেট করতে সাহায্য করবে, যা Google Authenticator অ্যাপ্লিকেশন ব্যবহার করে ব্যবহারকারীরা প্রমাণীকরণ করতে পারবেন।
ধাপ ২: Google Authenticator সেটআপ
১. Google Authenticator ব্যবহারকারীর জন্য একটি সিক্রেট কী তৈরি করা
আপনি ব্যবহারকারীকে একটি সিক্রেট কী দিতে হবে, যা তারা Google Authenticator অ্যাপ্লিকেশনে স্ক্যান করবেন। এই সিক্রেট কীটি একটি নির্দিষ্ট সময়ে TOTP তৈরি করবে।
import com.warrenstrange.googleauth.GoogleAuthenticator;
import com.warrenstrange.googleauth.GoogleAuthenticatorKey;
@Service
public class TwoFactorAuthService {
private final GoogleAuthenticator googleAuthenticator;
public TwoFactorAuthService() {
this.googleAuthenticator = new GoogleAuthenticator();
}
// Generate a secret key for a new user
public String generateSecretKey() {
GoogleAuthenticatorKey key = googleAuthenticator.createCredentials();
return key.getKey(); // secret key
}
}
- এখানে
generateSecretKey()মেথডে আমরা Google Authenticator জন্য সিক্রেট কী জেনারেট করছি।
২. ব্যবহারকারীকে QR কোড দেখানো
একবার সিক্রেট কী তৈরি হলে, এটি Google Authenticator অ্যাপ্লিকেশনে স্ক্যান করার জন্য একটি QR কোড আকারে প্রদান করা যেতে পারে।
import com.warrenstrange.googleauth.GoogleAuthenticator;
import com.warrenstrange.googleauth.GoogleAuthenticatorKey;
import com.warrenstrange.googleauth.GoogleAuthenticatorQRGenerator;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TwoFactorAuthController {
private final TwoFactorAuthService twoFactorAuthService;
public TwoFactorAuthController(TwoFactorAuthService twoFactorAuthService) {
this.twoFactorAuthService = twoFactorAuthService;
}
// Generate QR Code URL for the user to scan
@GetMapping("/generate-qr")
public String generateQrCode(@RequestParam String username) {
String secretKey = twoFactorAuthService.generateSecretKey();
String qrUrl = GoogleAuthenticatorQRGenerator.getOtpAuthURL("YourAppName", username, secretKey);
return qrUrl; // return the QR URL, can be rendered as image
}
}
GoogleAuthenticatorQRGenerator.getOtpAuthURL()ব্যবহারকারীর জন্য একটি QR কোড URL তৈরি করবে। এই URL-টি Google Authenticator অ্যাপ্লিকেশন দ্বারা স্ক্যান করা যাবে।
৩. TOTP যাচাই
যখন ব্যবহারকারী Google Authenticator অ্যাপ্লিকেশন থেকে OTP প্রদান করবে, তখন আপনাকে সেই OTP যাচাই করতে হবে।
import com.warrenstrange.googleauth.GoogleAuthenticator;
import org.springframework.stereotype.Service;
@Service
public class TwoFactorAuthService {
private final GoogleAuthenticator googleAuthenticator;
public TwoFactorAuthService() {
this.googleAuthenticator = new GoogleAuthenticator();
}
// Validate the OTP entered by the user
public boolean validateOtp(String secretKey, int otp) {
return googleAuthenticator.authorize(secretKey, otp); // validate the OTP
}
}
- এখানে,
validateOtp()মেথডে Google Authenticator থেকে পাওয়া OTP যাচাই করা হচ্ছে। যদি এটি সঠিক হয়, তাহলে প্রমাণীকরণ সফল হবে।
ধাপ ৩: Spring Security কনফিগারেশন
Spring Security তে লগইন প্রক্রিয়া কনফিগার করতে হবে যাতে 2FA যাচাই অন্তর্ভুক্ত থাকে।
২FA কনফিগারেশন এবং ফিল্টার তৈরি করা
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
@Configuration
public class SecurityConfig {
private final TwoFactorAuthService twoFactorAuthService;
public SecurityConfig(TwoFactorAuthService twoFactorAuthService) {
this.twoFactorAuthService = twoFactorAuthService;
}
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/login", "/generate-qr").permitAll()
.anyRequest().authenticated() // সমস্ত রিকোয়েস্টে অথেন্টিকেশন প্রয়োজন
.and()
.formLogin()
.loginPage("/login") // কাস্টম লগইন পেজ
.permitAll()
.and()
.addFilterBefore(new TwoFactorAuthFilter(twoFactorAuthService), UsernamePasswordAuthenticationFilter.class); // 2FA filter
return http.build();
}
}
এখানে, TwoFactorAuthFilter নামক একটি কাস্টম ফিল্টার ব্যবহার করা হচ্ছে, যা 2FA যাচাই প্রক্রিয়া সম্পন্ন করবে। এই ফিল্টারটি ব্যবহারকারী যখন প্রথমবার লগইন করবেন, তখন তাদের OTP যাচাই করার জন্য ব্যবহৃত হবে।
২FA ফিল্টার
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter("/login")
public class TwoFactorAuthFilter implements Filter {
private final TwoFactorAuthService twoFactorAuthService;
public TwoFactorAuthFilter(TwoFactorAuthService twoFactorAuthService) {
this.twoFactorAuthService = twoFactorAuthService;
}
@Override
public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws IOException, ServletException {
String otp = request.getParameter("otp");
String secretKey = "user-secret-key"; // সিক্রেট কীটি ডাটাবেস বা অন্য কোথাও সংরক্ষিত থাকে
if (otp != null) {
boolean isValid = twoFactorAuthService.validateOtp(secretKey, Integer.parseInt(otp));
if (isValid) {
chain.doFilter(request, response); // OTP সঠিক হলে লগইন সম্পন্ন হবে
} else {
response.sendRedirect("/login?error"); // ভুল OTP হলে লগইন ফেইল
}
} else {
response.sendRedirect("/2fa"); // OTP প্রবেশ করতে পেজে রিডাইরেক্ট
}
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {}
@Override
public void destroy() {}
}
এই ফিল্টারে, ব্যবহারকারী OTP জমা দেয়ার পরে তা যাচাই করা হয়। যদি OTP সঠিক হয়, তবে লগইন সফল হবে। যদি ভুল OTP হয়, ব্যবহারকারীকে একটি ত্রুটি পেজে রিডাইরেক্ট করা হবে।
উপসংহার
Google Authenticator ব্যবহার করে 2FA কনফিগার করার মাধ্যমে আপনি Spring Security অ্যাপ্লিকেশনে একটি শক্তিশালী দ্বৈত প্রমাণীকরণ ব্যবস্থা যুক্ত করতে পারেন। এটি ব্যবহারকারীর সুরক্ষা বাড়াতে সাহায্য করে, বিশেষ করে যখন তারা অস্বাভাবিক অবস্থানে লগইন করছে বা নতুন ডিভাইস থেকে অ্যাক্সেস করছে।